<?php
/**
 * 焦点图管理
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: FocusController.class.php v1.0 2014-06-23 17:27:03 $
 */
namespace Admin\Controller;
class FocusController extends CommonController{
    private $model;
    private $service;
    /**
     * 焦点图风格
     * @var type 
     */
    private $pattern = array(
        "mF_classicHC" => "mF_classicHC--经典怀旧系列一",
        "mF_classicHB" => "mF_classicHB--经典怀旧系列二",
        "mF_expo2010" => "mF_expo2010--2010世博主题",
        "mF_liuzg" => "mF_liuzg--绚丽切片风格",
        "mF_qiyi" => "mF_qiyi--奇艺主题",
        "mF_YSlider" => "YSlide--翻页效果",
        "mF_dleung" => "mF_dleung",
        "mF_51xflash" => "mF_51xflash",
        "mF_fancy" => "mF_fancy",
        "mF_fscreen_tb" => "mF_fscreen_tb",
        "mF_games_tb" => "mF_games_tb",
        "mF_kdui" => "mF_kdui",
        "mF_kiki" => "mF_kiki",
        "mF_ladyQ" => "mF_ladyQ",
        "mF_liquid" => "mF_liquid",
        "mF_luluJQ" => "mF_luluJQ",
        "mF_pconline" => "mF_pconline",
        "mF_peijianmall" => "mF_peijianmall",
        "mF_pithy_tb" => "mF_pithy_tb",
        "mF_quwan" => "mF_quwan",
        "mF_rapoo" => "mF_rapoo",
        "mF_shutters" => "mF_shutters",
        "mF_slide3D" => "mF_slide3D",
        "mF_sohusports" => "mF_sohusports",
        "mF_taobao2010" => "mF_taobao2010",
        "mF_taobaomall" => "mF_taobaomall",
        "mF_tbhuabao" => "mF_tbhuabao"
    );
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Focus");
        $this->service = D("Focus", "Service");
        $this->assign("pattern", $this->pattern);
    }
    
    /**
     * 列表
     */
    public function index() {
        $result = $this->getPagination('Focus');
        //var_dump($result);die;
        $this->assign('list', $result['data']);
        $this->assign('count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }
    
    /**
     * 添加
     */
    public function add() {
        //默认配置
        $focus = array(
            "pattern" => "mF_quwan" ,
            "times" => 2 ,
            "adTrigger" => "click" ,
            "auto" => 0 ,
            "width" => 450 ,
            "height" => 296 ,
            "txtHeight" => 30 ,
            "autozoom" => 1 
        );
        $this->assign('focus', $focus);
        $this->display();
    }
    
    /**
     * 创建
     * @return type
     */
    public function create() {
        $focus = I("post.focus", null);
        if(!isArrayAndNotEmpty($focus)){
            return $this->errorReturn("无效操作！");
        }
        
        // 添加数据
        $result = $this->service->add($focus);
        if (false === $result['status']) {
            return $this->errorReturn($result['data']['error']);
        }
        return $this->successReturn("添加焦点图成功！", U("index"));
    }
    
    /**
     * 修改
     */
    public function edit() {
        $id = I("get.id", 0, "intval");
        if($id <= 0){
            $this->error("无效操作！");
        }
        $focus = $this->service->getById($id);
        //var_dump($focus);die;
        if(!isArrayAndNotEmpty($focus)){
            return $this->errorReturn("无效操作！");
        }
        $this->assign('focus', $focus);
        //var_dump($focus);die;
        $this->display();
    }
    
    /**
     * 更新
     */
    public function update() {
        $focus = I("post.focus", null);
        if(!isArrayAndNotEmpty($focus)){
            return $this->errorReturn("无效操作！");
        }
        
        // 添加数据
        $result = $this->service->update($focus);
        if (false === $result['status']) {
            return $this->errorReturn($result['data']['error']);
        }
        
        return $this->successReturn("更新焦点图成功！", U("index"));
    }
    /**
     * 删除
     */
    public function delete() {
        $act = I("post.act", "one");
        if($act == "one"){
            $id = I("post.fid", 0, "intval");
            if($id <= 0){
                return $this->errorReturn("无效操作！");
            }
            $focus = $this->service->getById($id);
            if(!isArrayAndNotEmpty($focus)){
                return $this->errorReturn("无效操作！");
            }
            if($this->service->delete($id)){
               D("FocusImg", "Service")->deleteByFid($id);
                return $this->successReturn("删除成功", U("index"));
            }else{
                return $this->errorReturn("删除失败！");
            }
        }else if($act == "two"){
            $fids = I("post.fids");
            if(!isArrayAndNotEmpty($fids)){
                return $this->errorReturn("无效操作！");
            }
            foreach ($fids as $fid) {
                $focus = $this->service->getById($fid);
                if(!isArrayAndNotEmpty($focus)){
                    return $this->errorReturn("您要删除的数据不存在！");
                }
                $this->service->delete($fid);
                D("FocusImg", "Service")->deleteByFid($fid);
            }
            return $this->successReturn('删除数据成功！', U('index'));
        }else{
            return $this->errorReturn("无效操作！");
        }
    }
    
    /**
     * 预览
     */
    public function getcode(){
        $nid = I("get.nid", "", "trim");
        if($nid == ""){
            $this->error("无效操作");
        }
        $focus = $this->service->getByNid($nid);
        //var_dump($focus);die;
        if(!isArrayAndNotEmpty($focus)){
            $this->error("无效操作");
        }
        $url = U("Home/Focus/index", array("nid" => $nid));
        $code = "<script type=\"text/javascript\" src=\"{$url}\"></script>";
        $this->assign($focus);
        $this->assign("code", htmlentities($code));
        $this->display();
    }
}
